package com.zxy.house.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zxy.house.vo.HouseEntityVo;
import com.zxy.house.vo.HouseVo;
import com.zxy.house.vo.impl.HousesVo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zxy.house.entity.HouseEntity;
import com.zxy.house.service.HouseService;
import com.zxy.common.utils.PageUtils;
import com.zxy.common.utils.R;



/**
 * 房源表
 *
 * @author zb
 * @email sunlightcs@gmail.com
 * @date 2022-04-22 14:50:47
 */
@RestController
@RequestMapping("house/house")
@CrossOrigin
public class HouseController {
    @Autowired
    private HouseService houseService;

    /**
     * 列表信息
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = houseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{hid}")
    public R info(@PathVariable("hid") Integer hid){
		HouseEntity house = houseService.getById(hid);

        return R.ok().put("house", house);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody HouseEntity house){
		houseService.save(house);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HouseEntity house){
		houseService.updateById(house);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] hids){
		houseService.removeByIds(Arrays.asList(hids));

        return R.ok();
    }

    /**
     * 新房列表   1
     */
    @RequestMapping("/newHouseList")
    public R newHouseList(
            @RequestBody HouseVo houseVo,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "size",defaultValue = "3") Integer size
            ){
        List<HousesVo> housesVo  = houseService.newHouseList(houseVo,page,size);
        return R.ok().put( "list",housesVo );
    }

    /**
     * 爬虫添加数据
     */
    @RequestMapping("/saves")
    public R saves(@RequestBody HouseEntity house) throws Exception {
        //爬虫扒数据
        //链家二手房列表网址
        String link = "https://sh.lianjia.com/ershoufang/rs/";
        Document document = Jsoup.connect(link).get();

        Elements byClass = document.getElementsByClass("sellListContent");

        Elements li = byClass.select( "li" );

        for (int i = 0; i < 10; i++) {
            String attr = li.get(i).select("a").attr("href");
            String attrContent = "房源详情："+attr;

            Element info = li.select("div.info.clear").get(i);

            String title = info.select("div.title").text();
            String titleContent = "房源标题："+title;
            house.setHTitle( title );

            String addr = info.select("div.address").text();
            String addrContent = "房源楼层："+addr;

            String louceng = info.select("div.flood").text();
            String loucengContent = "房源地址："+louceng;
            house.setHArea( louceng );

            String text = info.select( "div.followInfo" ).text();
            String tests = "关注人数/发布时间："+text;


            String text1 = info.select( "div.tag" ).text();
            String text11 = "看房：" + text1 ;

            house.setHState( 0 );

            houseService.save(house);

        }

        return R.ok();
    }

}
